<template>
  <div>
    <!--面包屑-->
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size:18px ">
      <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-s-promotion"></i> 后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>类别列表</el-breadcrumb-item>
      <el-breadcrumb-item v-for="item in history" :key="item.id">{{ item.name }}</el-breadcrumb-item>
    </el-breadcrumb>
    <!--分割线-->
    <el-divider style="margin-bottom: 50px"></el-divider>
    <!--具体显示的表格-->
    <el-table
        :data="tableData"
        style="width: 100%" highlight-current-row>
      <!--至少又一列时不设置宽度的，因为不设置宽度会占据到整行的所有数据-->
      <!--      <el-table-column type="index"></el-table-column>-->
      <el-table-column prop="id" label="ID" width="80" header-align="center" align="center"></el-table-column>
      <el-table-column prop="name" label="分类名称" width="150" align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="keywords" label="关键字列表" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="sort" label="分类级别" header-align="center" align="center" width="150"></el-table-column>
      <el-table-column label="是否启用" header-align="center" align="center" width="150">
        <!--获取表格当前行对应的数据对象-->
        <template slot-scope="scope">
          <!--将此标签的值和当前行对应的数据对象中的enable属性进行绑定，
          :active-value="1" 表示此开关打开时为 1 :表示我们设定的"1"表示一个数字
           :inactive-value="0" 关闭时为0
          -->
          <el-switch
              v-model="scope.row.enable"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ccc"
              @change="changeStatus(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="是否显示在导航栏" header-align="center" align="center" width="150">
        <template slot-scope="scope">
          <el-switch
              v-model="scope.row.isDisplay"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ccc"
              @change="changeDisplay(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="子级类别" width="120" align="center">
        <!--scope表示当前表-->
        <template slot-scope="scope">
          <!--根据当前行数据对象中的isParent属性来确认当前按钮的禁用状态-->
          <el-button size="mini" :disabled="scope.row.isParent != 1"
                     @click="showSubCategoryList(scope.row)">
            查看子分类
          </el-button>
        </template>
      </el-table-column>
      <el-table-column width="100" label="操作" header-align="center" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini"
                     @click="showEditFormDialog(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle size="mini" :disabled="scope.row.isParent == 1"
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--如果当前显示的分类信息的父类ID不为0时显示返回按钮    -->
    <div style="margin-top:10px;text-align: right" v-if="currentParentId != 0">
      <el-button size="medium" type="primary" @click="goBack()"> 返回</el-button>
    </div>
  </div>
</template>

<script>
export default {
  name: "CategoryListView",
  data() {
    return {
      //绑定表格数据的数组
      tableData: [],
      //当点击查看子分类按钮时，会将当前行对象按照深度存放在此数组当中
      history: [],
      //当前正在使用的父类ID
      currentParentId: 0,
    }
  }, methods: {
    //设置分类导航的启用状态
    changeDisplay(category) {
      let url = "http://localhost:15722/categorys/" + category.id;
      console.log(category.isDisplay);
      //开关从开启->关闭 传递过来的就是0 关闭->开启 传递过来的就是1
      if (category.isDisplay == 0) {
        url += "/forbidden-display";
      } else {
        url +=  "/start-display";
      }
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let jsonResult = response.data;
        let text = ["禁用", "启用"];
        if (jsonResult.state == 20000) {
          this.$message({
            type: 'success',
            message: text[category.isDisplay] + '【' + category.name + '】品牌状态成功!'
          });
          this.loadCategoryList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '修改失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadCategoryList();
              this.dialogFormVisible = false;
            }
          });
        } else if (jsonResult.state == 40900) {
          this.$message.error(jsonResult.message);
        } else {
          this.$alert(jsonResult.message, '修改失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadCategoryList();
              this.dialogFormVisible = false;
            }
          });
        }
      });
    },
    //设置分类的启用状态
    changeStatus(category) {
      let url = "http://localhost:15722/categorys/" + category.id;
      console.log(category.enable);
      //开关从开启->关闭 传递过来的就是0 关闭->开启 传递过来的就是1
      if (category.enable == 0) {
        url += "/disable";
      } else {
        url +=  "/enable";
      }
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let jsonResult = response.data;
        let text = ["禁用", "启用"];
        if (jsonResult.state == 20000) {
          this.$message({
            type: 'success',
            message: text[category.enable] + '【' + category.name + '】品牌状态成功!'
          });
          this.loadCategoryList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '修改失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadCategoryList();
              this.dialogFormVisible = false;
            }
          });
        } else if (jsonResult.state == 40900) {
          this.$message.error(jsonResult.message);
        } else {
          this.$alert(jsonResult.message, '修改失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadCategoryList();
              this.dialogFormVisible = false;
            }
          });
        }
      });
    },
    //显示修改需要弹出的Dialog
    showEditFormDialog(category) {
      // this.dialogFormVisible=true;
      // this.editForm = category;
      // console.log("这是编辑按钮的：" + category.id);

      //通过发送请求判断数据是否存在
      let url = "http://localhost:15722/categorys/" + category.id;
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.editForm = jsonResult.data;
          this.dialogFormVisible = true;
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '数据查询失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadCategoryList();
            }
          });
        } else {
          this.$alert(jsonResult.message, '数据查询失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadCategoryList();
            }
          });
        }
      });
    },
    //在修改弹窗中，点击确认触发此方法
    submitEditForm() {
      let url = "http://localhost:15722/categorys/" + this.editForm.id + "/update";
      let forData = this.qs.stringify(this.editForm);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url, forData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            type: 'success',
            message: '修改【' + this.editForm.name + '】相册成功!'
          });
          this.loadCategoryList();
          this.dialogFormVisible = false;
        } else if (jsonResult.state == 40900) {
          this.$message.error(jsonResult.message);
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '修改失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadCategoryList();
              this.dialogFormVisible = false;
            }
          });
        } else {
          this.$alert(jsonResult.message, '修改失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadCategoryList();
              this.dialogFormVisible = false;
            }
          });
        }
      });
    },
    //打开删除提示
    openDeleteConfirm(category) {
      let message = '此操作将永久删除【' + category.name + '】分类，是否继续?'
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        /*成功后触发此方法*/
        this.handleDelete(category);
      }).catch(() => {
        /*取消之后进行的处理*/
      });
      console.log("这是删除按钮的：" + category.id);
    },
    //处理删除
    handleDelete(category) {
      let url = "http://localhost:15722/categorys/" + category.id + "/delete";
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            type: 'success',
            message: '删除【' + category.name + '】分类成功!'
          });
          //刷新列表
          this.loadCategoryList();
        } else if (jsonResult.state == 40900) {
          this.$alert(jsonResult.message, '删除失败', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        } else {
          this.$alert(jsonResult.message, '删除失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadCategoryList();
            }
          });
        }
      });
    },
    /**
     * 返回到上一个分类列表
     * */
    goBack() {
      //history中依次存放着每次查看子分类时的分类数据并且按照深度存放在对应的下标位置
      //获取到history数组中的最后一个对象(最后一个对象就代表着我们当前所查看的分类的父类)
      let parentCategory = this.history[this.history.length - 1];
      //删除数组中的最后一个元素
      this.history.pop();
      //根据刚刚获取到的对象获取其父类ID，并且赋值给currentParenId属性
      this.currentParentId = parentCategory.parentId;
      this.loadCategoryList();
    },
    /**
     * 加载子类分类列表
     * */
    showSubCategoryList(category) {
      //将触发此方法的行中的数据对象按照此数据对象的深度将此对象存放在此数组的对应下标位置
      this.history[category.depth - 1] = category;
      this.currentParentId = category.id;
      this.loadCategoryList();
    },
    /**
     * 加载分类列表
     */
    loadCategoryList() {
      let url = "http://localhost:15722/categorys/lis-by-parent?parentId=" + this.currentParentId;
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        this.tableData = jsonResult.data;
        //打印测试
        // console.log(this.tableData);
      });
    }
  }, mounted() {
    this.loadCategoryList();
  }

}
</script>

<style scoped>

</style>